package com.jk.customer.controller;

import com.jk.customer.entity.*;
import com.jk.customer.service.ProductService;
import com.jk.customer.utils.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 主表查询
     * @param product
     * @return
     * 陈浩
     */
    @RequestMapping("findProduct")
    @ResponseBody
    public List<ProductEntity> findProduct(ProductEntity product,HttpServletRequest request){
        return productService.findProduct(product,request);
    }

    /**
     * 取消询价单
     * @param productId
     * 陈浩
     */
    @RequestMapping("cancelEnquiry")
    @ResponseBody
    public void cancelEnquiry(String productId){
        productService.cancelEnquiry(productId);
    }

    /**
     * 重新发起
     * @param productId
     * 陈浩
     */
    @RequestMapping("resend")
    @ResponseBody
    public void resend(String productId){
        productService.resend(productId);
    }

    /**
     * 延期回显
     * @param productId
     * @return
     * 陈浩
     */
    @RequestMapping("postpone")
    @ResponseBody
    public PriceEntity postpone(String productId){
        return productService.postpone(productId);
    }

    /**
     * 公司二级联动
     * @return
     * 陈浩
     */
    @RequestMapping("getSelectClassChr")
    @ResponseBody
    public List<PurchasingDepartmentOrganization> getSelectClassChr(){
        return productService.getSelectClassChr();
    }

    /**
     * 部门二级联动
     * @param pid
     * @return
     * 陈浩
     */
    @RequestMapping("getSelectClassChrNb")
    @ResponseBody
    public List<PurchasingDepartmentOrganization> getSelectClassChrNb(Integer pid){
        return productService.getSelectClassChrNb(pid);
    }

    /**
     * 工厂二级联动
     * @param pid
     * @return
     * 陈浩
     */
    @RequestMapping("getSelectClassChrNb2")
    @ResponseBody
    public List<PurchasingDepartmentOrganization> getSelectClassChrNb2(Integer pid){
        return productService.getSelectClassChrNb2(pid);
    }

    /**
     * 库存二级联动
     * @param pid
     * @return
     * 陈浩
     */
    @RequestMapping("getSelectClassChrNb3")
    @ResponseBody
    public List<PurchasingDepartmentOrganization> getSelectClassChrNb3(Integer pid){
        return productService.getSelectClassChrNb3(pid);
    }

    /**
     *
     * @param queryPriceAccessory
     * 上传图片OSS
     * @return
     * @throws Exception
     * 陈浩
     */
    @RequestMapping("upLoadChr")
    @ResponseBody
    public Object upLoadChr(MultipartFile queryPriceAccessory) throws Exception {
        String s = OSSUtil.upload2oss(queryPriceAccessory);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("queryPriceAccessory",s);
        return map;
    }

    /**
     * 新增
     * @param price
     * 陈浩
     */
    @RequestMapping("addProduct_Chr")
    @ResponseBody
    public void addProduct_Chr(PriceEntity price, HttpServletRequest request){
        productService.addProduct_Chr(price,request);
    }

    /**
     * 新增
     * @param id
     * @param queryEndDate
     */
    @RequestMapping("save")
    @ResponseBody
    public void save(String id,String queryEndDate){
        productService.save(id,queryEndDate);
    }
    @RequestMapping("toFindProduct")
    public String toFindProduct(){
        return "findProduct";
    }

    /**
     * 查询
     * @param id
     * @return
     */
    @RequestMapping("showPriceChr1")
    @ResponseBody
    public List<OfferFormEntity> showPriceChr(String id){
        return productService.showPriceChr(id);
    }

    @RequestMapping("selectEnquiry")
    public String selectEnquiry(String productId, Model model){
        PriceEntity priceEntity = new PriceEntity();
        priceEntity.setQueryPriceId(productId);

        model.addAttribute("id",priceEntity);
        return "showPriceChr";
    }

    /**
     * 查看报价
     * @param productId
     * @param model
     * @return
     */
    @RequestMapping("selectQuotedChr")
    public String selectQuotedChr(String productId,String name, Model model,HttpServletRequest request){
        PriceEntity priceEntity = new PriceEntity();
        priceEntity.setQueryPriceId(productId);
        priceEntity.setQueryPictureId(name);
        UserEntity user = (UserEntity) request.getSession().getAttribute("user");
        priceEntity.setAddress(user.getUserRealName());


        model.addAttribute("id",priceEntity);
        return "selectQuotedChr";
    }
    /**
     * 查看明细
     * @param id
     * @return
     * 陈浩
     */
    @RequestMapping("selectQuotedChr1")
    @ResponseBody
    public MaterialsEntity selectQuotedChr(String id,String name,String name2){

        return productService.selectQuotedChr(id,name,name2);
    }

    /**
     * 回显价格
     * @param id
     * @return
     */
    @RequestMapping("selectQuotedChr2")
    @ResponseBody
    public OfferFormEntity selectQuotedChr2(String id,String name){
        return productService.selectQuotedChr2(id,name);
    }

    /**
     * model换取Id传至前台页面
     * @param nbCh
     * @param model
     * @return
     */
    @RequestMapping("addNewZeng")
    public String addNewZeng(String nbCh,String id, Model model,HttpServletRequest request){
        PriceEntity priceEntity = new PriceEntity();
        priceEntity.setQueryPriceId(nbCh);
        priceEntity.setAddress(id);
        UserEntity user = (UserEntity) request.getSession().getAttribute("user");
        priceEntity.setQueryPriceRequirementType(user.getUserRealName());
        model.addAttribute("nbCh",priceEntity);
        return "addNewZeng";
    }

    @RequestMapping("selectDingDan")
    public String selectDingDan(String productId, Model model){
        PriceEntity priceEntity = new PriceEntity();
        priceEntity.setQueryPriceId(productId);

        model.addAttribute("id",priceEntity);
        return "queryOrder";
    }

    /**
     * 确定中标回显材料详情
     * @param nbCh
     * @return
     */
    @RequestMapping("selectHuiChr")
    @ResponseBody
    public MaterialsEntity selectHuiChr(String nbCh,String nbCh2,String nbCh3){
        return productService.selectHuiChr(nbCh,nbCh2,nbCh3);
    }

    /**
     * MOdel取Id修改回显询价表皮用
     * @param productId
     * @param model
     * @return
     */
    @RequestMapping("updateEnquiry")
    public String updateEnquiry(String productId,Model model){
        PriceEntity priceEntity = new PriceEntity();
        priceEntity.setQueryPriceId(productId);

        model.addAttribute("id",priceEntity);

        return "findCustomized";
    }

    @RequestMapping("priceParity")
    public String priceParity(String id,Model model){
        PriceEntity priceEntity = new PriceEntity();
        priceEntity.setQueryPriceId(id);

        model.addAttribute("id",priceEntity);

        return "priceParity";
    }


    /**
     * 提交订单
     * @param totalPrice
     * @param order
     */
    @RequestMapping("submitOrder")
    @ResponseBody
    public void submitOrder(Integer totalPrice,OrderFormEntity order,String nbCh2,String nbCh){
        productService.submitOrder(totalPrice,order,nbCh2,nbCh);
    }

    @RequestMapping("addFindPriceChr")
    public String addFindPriceChr(String id,Model model,HttpServletRequest request){
        PriceEntity priceEntity = new PriceEntity();
        priceEntity.setQueryPriceId(id);
        request.getSession().setAttribute("id",id);
        model.addAttribute("id",priceEntity);

        return "addFindPriceChr";
    }

    /**
     * 回显修改询价表单
     * @param productId
     * @return
     */
    @RequestMapping("updateSelectQuery")
    @ResponseBody
    public PriceEntity updateSelectQuery(String productId){
        return productService.updateSelectQuery(productId);
    }

    @RequestMapping("updateSelectQuerySave")
    @ResponseBody
    public void updateSelectQuerySave(ProductEntity price,HttpServletRequest request){
        String id = (String) request.getSession().getAttribute("id");
        price.setProductId(id);
        if (price.getSelectQueryPriceStartDate() == 1){
            Date date = new Date();
            SimpleDateFormat ww= new SimpleDateFormat("yyyy-MM-dd");
            String format = ww.format(date);
            price.setQueryPriceStartDate(format);
        }
        if (price.getSelectBiddingEndDate() == 1){
            Date date = new Date();
            SimpleDateFormat ww= new SimpleDateFormat("yyyy-MM-dd");
            String format = ww.format(date);
            price.setBiddingEndDate(format);
        }

        productService.updateSelectQuerySave(price);
    }
    @RequestMapping("findTotalPages_findPrice")
    @ResponseBody
    public List<ProductEntity> findTotalPages_findPrice(ProductEntity product){
        return productService.findTotalPages_findPrice(product);
    }
}
